import React, { Component } from 'react';
import './App.css';
import eosc from './api/eosc_api';
import {get_scatter_identity,vote,newaccount} from './api/scatter_api';
import {toAsset} from './api/utils'
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {infoData: {}};
  }
  getinfo(){
    eosc.getInfo({}).then(res => {

      console.log(res);
      this.setState({
        infoData: res
      });
  });
  }
  getActions()
  {
    eosc.getActions({ 
      account_name: 'eosdeveloper', 
      pos: 0, 
      offset: 10, 
      limit: 100 
  })
  .then(res => {
      console.log(res);
  })  

  }
   getScatterIdentity()
  {
   
     

  } 
  async sc_newAccount()
  {
    
    
  }

  async sc_transfer (){

    get_scatter_identity().then(
     async (identity) => {
      var eos=identity.data.eos;
      var permission=identity.data.permission;
      var account_name=identity.data.account_name;
    console.log(identity.data);
    let amount=0.001;
let to="sx";
let memo="scatter test ";
let tokenSymbol="EOS";
 //获取到的scatter的eos对象
let token = await eos.contract(tokenSymbol === 'EOS' ? 'eosio' : 'eosio.token').then(token => { return token });
console.log(token);

token.transfer(account_name, to, toAsset(amount), memo, permission)
                .then(data => {
                  console.log('转账成功');
                  console.log(data);
                    return {
                        is_error: false,
                        data
                    }
                });
              });

  
  }
   componentDidMount() {
    console.log('componentDidMount');
    newaccount("m2dsaount","EOS6tWDBqV3FSP9fqeKuar9eiUUtnbZLeS5TTAC8q8kU4AjUFj8f5");
    //this.getinfo();
    //21457 //实际数量 否则变成赎回
    //vote(toAsset(21456),'eosco')
    //领取分红
  
      //获取到的scatter的eos对象
      // eos.claim(account_name, 'eosgod', permission)
      // .then(data => {
      //   console.log('claim success');
      //   console.log(data);
      //     return {
      //         is_error: false,
      //         data
      //     };
      // })
      // .catch(err => {
      //     return {
      //         is_error: true,
      //         msg: err
      //     };
      // });
      //this.sc_transfer();


    //获取到scatter配置d的account
    // const account = identity.accounts.find(function(acc){
    //     return acc.blockchain === 'eos';
    // });
    //account.authority 是permission
    //选择的用户名account.name
    //创建操作的配置
    // let options = {
    //      authorization: account.name+'@'+account.authority,
    //      broadcast: true,
    //      sign: true
    // }
    //获取eos对象
   // let eos = scatter.eos(network, Eos,  options, "https");

    //this.getActions();
  }
  render() {

   var infoJson =JSON.stringify(this.state.infoData);
    
    return (
      <div className="App">
      <h1>eosc api</h1>
      <ul>
        <li>
        chain_id:{this.state.infoData.chain_id}
        </li>
        <li>
        head_block_num:{this.state.infoData.chain_id}
        </li>
      </ul>
      </div>
    );
  }
}

export default App;
